package de.jmda.home.ui.vaadin.usermgmt;

import javax.ejb.EJB;

import org.vaadin.virkki.cdiutils.application.UIContext.UIScoped;

import de.jmda.cbo.user.User;
import de.jmda.common.ui.web.vaadin.service.DefaultSessionContext;
import de.jmda.home.bl.usermgmt.UserManagementBean;

@UIScoped // virkii version
public class AuthenticatorDB implements Authenticator
{
	/**
	 * TODO make use of CDI as soon as possible
	 */
	@EJB
	private UserManagementBean userManagement =
			DefaultSessionContext.get().getUserManagement();

	@Override
	public String authenticate(String username, String password)
	{
		User user = userManagement.findUserByUsername(username);

		if (user == null)
		{
			return
					userManagement.findRegistrationRequestUsernameByUsernameRegistrationCode(
							username, password);
		}

		return user.getUsername();
	}
}